﻿using Basic.FormCollect;
using Basic.FormModel.DB;
using Basic.FormModel.PhyQuery;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.PhysicalQuery.Model;
using Basic.FormService.Interface;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;

namespace Basic.FormService.lmpl
{
    internal class PhysicalQueryService : IPhysicalQueryService
    {
        private readonly IPhysicalQueryCollect _Service;
        private readonly ICustomFormCollect _Form;
        private readonly IQueryControlCollect _Control;
        private readonly IQueryColumnCollect _Column;
        public PhysicalQueryService ( IPhysicalQueryCollect service,
            ICustomFormCollect form,
            IQueryColumnCollect column,
            IQueryControlCollect control )
        {
            this._Column = column;
            this._Control = control;
            this._Form = form;
            this._Service = service;
        }

        public long Add ( PhysicalQueryAdd data )
        {
            PhyQueryAdd add = data.ConvertMap<PhysicalQueryAdd, PhyQueryAdd>();
            return this._Service.Add(add);
        }


        public void Delete ( long id )
        {
            DBPhysicalQueryLayout sour = this._Service.Get(id);
            this._Service.Delete(sour);
        }

        public PhysicalQueryDto Get ( long id )
        {
            DBPhysicalQueryLayout sour = this._Service.Get(id);
            return sour.ConvertMap<DBPhysicalQueryLayout, PhysicalQueryDto>();
        }

        public PhysicalQueryBody GetBody ( long formId )
        {
            DBCustomForm form = this._Form.Get(formId);
            if ( form.FormType != FormType.物理表单 )
            {
                throw new ErrorException("form.type.error");
            }
            DBPhysicalQueryLayout[] list = this._Service.GetsByFormId<DBPhysicalQueryLayout>(formId);
            PhysicalQueryBody body = new PhysicalQueryBody
            {
                FormName = form.FormName,
                Layout = form.Layout,
            };
            if ( ( FormLayoutType.左 & form.Layout ) == FormLayoutType.左 )
            {
                body.Left = list.ConvertFind<DBPhysicalQueryLayout, PhysicalQueryDto>(a => a.Location == LayoutLocation.左边);
            }
            if ( ( FormLayoutType.搜索栏 & form.Layout ) == FormLayoutType.搜索栏 )
            {
                body.Search = list.ConvertMap<DBPhysicalQueryLayout, PhysicalQueryDto>(a => a.Location == LayoutLocation.搜索栏 && a.IsMoreFilter == false);
                body.Filter = list.ConvertMap<DBPhysicalQueryLayout, PhysicalQueryDto>(a => a.Location == LayoutLocation.搜索栏 && a.IsMoreFilter);
            }
            return body;
        }

        public PhysicalQueryData[] Gets ( long formId, LayoutLocation location )
        {
            PhyQueryDto[] list = this._Service.GetsByFormId<PhyQueryDto>(formId, location);
            if ( list.IsNull() )
            {
                return null;
            }
            Dictionary<long, string> controls = this._Control.GetNames(list.ConvertAll(a => a.ControlId));
            List<long> colId = new List<long>();
            list.ForEach(c =>
            {
                colId.AddRange(c.ColumnId);
            });
            Dictionary<long, string> colName = this._Column.GetLabels(colId.Distinct().ToArray());
            PhysicalQueryData[] dtos = list.ConvertMap<PhyQueryDto, PhysicalQueryData>();
            dtos.ForEach(c =>
            {
                c.ControlName = controls.GetValueOrDefault(c.ControlId);
                if ( !c.ColumnId.IsNull() )
                {
                    c.ColName = c.ColumnId.Join(',', a => colName[a]);
                }
            });
            return dtos;
        }

        public PhysicalQueryDto[] GetsByFormId ( long formId )
        {
            DBPhysicalQueryLayout[] list = this._Service.GetsByFormId<DBPhysicalQueryLayout>(formId);
            return list.ConvertMap<DBPhysicalQueryLayout, PhysicalQueryDto>();
        }

        public void SetSort ( KeyValuePair<long, int>[] sets )
        {
            this._Service.SetSort(sets);
        }

        public bool Update ( long id, PhysicalQuerySet set )
        {
            PhyQuerySet data = set.ConvertMap<PhysicalQuerySet, PhyQuerySet>();
            DBPhysicalQueryLayout sour = this._Service.Get(id);
            return this._Service.Update(sour, data);
        }
    }
}
